/**
 * BaseServlet.java
 * 
 * Copyright(C)2010 FOUNDER CORPORATION
 * 
 */

package org.iori.luodan.base;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.iori.common.log.FounderLog;
import com.iori.common.log.LogFactory;

/**
 * [クラス名]<br>
 * BaseServlet.java<br>
 * [機能概要]<br>
 * <br>
 * <br>
 * [変更履歴]<br>
 * 2010/11/10 ver1.00 新規作成<br>
 * 
 * @author FOUNDER CORPORATION
 * @version 1.00
 */
public abstract class BaseServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7499998285608670178L;
	
	//Log出力クラスを定義
	private static FounderLog logger = LogFactory
			.getFounderLog(BaseServlet.class);

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doMethod(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doMethod(request, response);
	}

	/**
	 * 共通の処理ロジック
	 * 　　　ログイン中判断
	 * 　　　jspに遷移
	 * 　　　セッションを取得する
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doMethod(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// ログ
		//logger.info("LOG_MSG_5001",request.getRequestURI());
		//セッション取得
		HttpSession session = request.getSession(false);
		if (null == session) {
			session = request.getSession(true);
		}

		//遷移先
		String forwardURL = null;
		request.getContextPath();
		String forward = null;
		/**
		 * TODO 遷移先を取得 実際の処理ロジック
		 */
		try {
			forward = this.doExcute(request, response);
		} catch (Exception e) {
			//TODO 例外処理
			System.out.println(e.getMessage());

		} finally {
			if (null == forward) {
				return;
			}
			if (forward.contains(".jsp")) {
				forwardURL = "/" + forward;
			} else {
				forwardURL = forward;
			}
			//logger.info("LOG_MSG_5002",forwardURL);
			/**
			 * TODO 遷移を行う
			 */
			ServletContext sc = getServletContext();
			// RequestDispatcher方法で遷移
			RequestDispatcher rd = sc.getRequestDispatcher(forwardURL);
			rd.forward(request, response);
		}
		return;
	}

	/**
	 * ロジック処理実装のため抽象メソッドが継承される
	 * 
	 * @param request
	 * @param response
	 * @return 遷移先のJSPのパス
	 * @throws Exception
	 */
	protected abstract String doExcute(HttpServletRequest request,
			HttpServletResponse response) throws Exception;

	/**
	 * 
	 * <br>
	 * @param request
	 */
}